//
//  YWEquipmentManager.m
//  maintain
//
//  Created by 郑想 on 15/10/26.
//  Copyright © 2015年 ewin. All rights reserved.
//

#import "MJExtension.h"
#import "WMHttpTool.h"
#import "YWCommonTool.h"
#import "YWDBTool.h"
#import "YWDictEquipmentRule.h"
#import "YWDictEquipmentRule.h"
#import "YWDictMaintenanceType.h"
#import "YWEquipmentManager.h"
#import "YWEquipmentRuleRel.h"
#import "YWEquipmentRulesJson.h"
#import "YWLastMaintenanceTime.h"
#import "YWMacro.h"
#import "YWNotifyManager.h"
#import "YWQrcodeInfo.h"
#import "YWbuildingJson.h"
#import "YWBuildingManager.h"
#import "YWEquipmentType.h"

@interface YWEquipmentManager ()
@end

@implementation YWEquipmentManager
YWSingletonM(Manager)

#pragma mark - EquipmentType methods
- (BOOL)saveEquipmentTypeWithArray:(NSArray*)equipmentTypeArray
{
    return [[YWDBTool shareDBTool] insertDictEquipmentTypeWithArray:equipmentTypeArray];
}

- (NSArray *)allEquipmentTypeWithSystemId:(NSInteger)systemTypeId;{
    return [[YWDBTool shareDBTool] allEquipmentTypeWithSystemId:systemTypeId];
}

- (NSArray *)allEquipmentSystemType{
    return [[YWDBTool shareDBTool] allEquipmentSystemType];
}

- (NSArray *)allEquipmentSubSystemType{
    return [[YWDBTool shareDBTool] allEquipmentSubSystemType];
}

- (YWEquipmentSystemType*)equipmentSystemTypeWithEquipmentTypeId:(NSInteger)equipmentTypeId{
    return [[YWDBTool shareDBTool] equipmentSystemTypeWithEquipmentTypeId:equipmentTypeId];
}

- (NSTimeInterval)dicEquipmentRuntimeParameterMaxCreateTime{
    return [[YWDBTool shareDBTool] dicEquipmentRuntimeParameterMaxCreateTime];
}

- (BOOL)saveRuntimeParameterWithArray:(NSArray*)runtimeParameterArray
{
    return [[YWDBTool shareDBTool] saveRuntimeParameterWithArray:runtimeParameterArray];
}

#pragma mark - EquipmentRule methods
- (void)saveEquipmentRulesWithArray:(NSArray*)equipmentRulesArray
{
    NSMutableArray* equipmentRuleRelArray = [NSMutableArray array];
    NSMutableArray* equipmentRuleArray = [NSMutableArray array];

    [equipmentRulesArray enumerateObjectsUsingBlock:^(YWEquipmentRulesJson* _Nonnull equipmentRuleJson, NSUInteger idx, BOOL* _Nonnull stop) {

        YWEquipmentRuleRel* rel = [[YWEquipmentRuleRel alloc] init];
        rel.equipmentTypeId = equipmentRuleJson.equipmentTypeId;
        rel.maintenanceTypeId = equipmentRuleJson.maintenanceTypeId;
        rel.maintenanceCycleId = equipmentRuleJson.maintenanceCycleId;
        rel.equipmentRuleId = equipmentRuleJson.equipmentRuleId;
        [equipmentRuleRelArray addObject:rel];

        YWDictEquipmentRule* rule = [[YWDictEquipmentRule alloc] init];
        rule.equipmentRuleId = equipmentRuleJson.equipmentRuleId;
        rule.content = equipmentRuleJson.content;
        [equipmentRuleArray addObject:rule];
    }];

    [[YWDBTool shareDBTool] insertEquipmentRuleRelWithArray:equipmentRuleRelArray];
    [[YWDBTool shareDBTool] insertDictEquipmentRuleWithArray:equipmentRuleArray];
}

- (NSArray*)rulesWithEquipmentTypeId:(NSInteger)equipmentTypeId maintenanceTypeId:(NSInteger)maintenanceTypeId maintenanceCycleId:(NSInteger)maintenanceCycleId
{
    NSArray* rules = [[YWDBTool shareDBTool] rulesWithEquipmentTypeId:equipmentTypeId maintenanceTypeId:maintenanceTypeId maintenanceCycleId:maintenanceCycleId];
    if (equipmentTypeId && rules.count == 0) {
        YWLog(@"暂时不下载规则,equipmentTypeId:%ld",(long)equipmentTypeId);
    }
//    if (equipmentTypeId && rules.count == 0) {
//        YWLog(@"下载规则,equipmentTypeId:%ld",(long)equipmentTypeId);
//        NSDictionary *dic = [YWCommonTool addSubHttpHeadWithDic:[WMHttpTool shareHttpTool].HTTPHeaders];
//        [WMHttpTool shareHttpTool].HTTPHeaders = dic;
//        if (dic == nil) {return rules;}
//        [[WMHttpTool shareHttpTool] getWithUrl:[YWCommonTool getSingleEquipmentRuleUrl] params:@{ @"equipmentTypeId" : @(equipmentTypeId),@"maintenanceTypeId" : @(maintenanceTypeId) }success:^(NSHTTPURLResponse* response, id json) {
//            NSArray* equipmentRulesJsons = [YWEquipmentRulesJson mj_objectArrayWithKeyValuesArray:json];
//            if (equipmentRulesJsons.count) {
//                YWLog(@"下载规则成功");
//                [equipmentRulesJsons enumerateObjectsUsingBlock:^(YWEquipmentRulesJson* equipmentRulesJson, NSUInteger idx, BOOL* _Nonnull stop) {
//                    YWEquipmentRuleRel* rel = [[YWEquipmentRuleRel alloc] init];
//                    rel.equipmentTypeId = equipmentRulesJson.equipmentTypeId;
//                    rel.maintenanceTypeId = equipmentRulesJson.maintenanceTypeId;
//                    rel.maintenanceCycleId = equipmentRulesJson.maintenanceCycleId;
//                    rel.equipmentRuleId = equipmentRulesJson.equipmentRuleId;
//                    
//                    YWDictEquipmentRule* dictEquipmentRule = [[YWDictEquipmentRule alloc] init];
//                    dictEquipmentRule.equipmentRuleId = equipmentRulesJson.equipmentRuleId;
//                    dictEquipmentRule.content = equipmentRulesJson.content;
//                    
//                    [[YWDBTool shareDBTool] updateEquipmentRuleRel:rel];
//                    [[YWDBTool shareDBTool] updateDictEquipmentRule:dictEquipmentRule];
//                }];
//            }
//        } failure:^(NSHTTPURLResponse* response, NSError* error) {
//            YWLog(@"%s,error:%@", __func__, error.localizedDescription);
//        }];
//    }
    return rules;
}

//#pragma mark - DictMaintenanceType methods
//- (void)saveDictMaintenanceTypesWithArray:(NSArray*)dictMaintenanceTypesArray
//{
//    [[YWDBTool shareDBTool] insertDictMaintenanceTypeWithArray:dictMaintenanceTypesArray];
//}
//
//- (NSArray*)maintenanceTypes
//{
//    return [[YWDBTool shareDBTool] maintenceTypes];
//}

- (NSString*)equipmentTypeNameWithTypeId:(NSInteger)equipmentTypeId
{
    return [[YWDBTool shareDBTool] equipmentTypeNameWithTypeId:equipmentTypeId];
}

- (NSTimeInterval)dicEquipmentTypeMaxUpdateTime{
    return [[YWDBTool shareDBTool] dicEquipmentTypeMaxUpdateTime];
}

- (NSArray *)equipmentFieldsWithTypeId:(NSInteger)equipmentTypeId maintenanceTypeId:(NSInteger)maintenanceTypeId{
    return [[YWDBTool shareDBTool] equipmentFieldsWithTypeId:equipmentTypeId maintenanceTypeId:maintenanceTypeId];
}

- (NSArray *)equipmentParametersWithEquipmentId:(NSString*)equipmentId{
    return [[YWDBTool shareDBTool] equipmentParametersWithEquipmentId:equipmentId];
}

#pragma mark - Equipment methods
- (BOOL)isExistEquipmentWithQrcodeId:(NSString*)qrcodeId
{
    return [[YWDBTool shareDBTool] isExistEquipmentWithQrcodeId:qrcodeId];
}

- (BOOL)isExistEquipmentWithLocationId:(NSInteger)locationId{
    return [[YWDBTool shareDBTool] isExistEquipmentWithLocationId:locationId];
}

- (void)saveEquipmentsWithArray:(NSArray*)equipmentsArray
{
    [[YWDBTool shareDBTool] insertEquipmentWithArray:equipmentsArray];
}

- (void)saveQrcodeEquipmentsWithArray:(NSArray *)equipmentsArray{
    [[YWDBTool shareDBTool] saveQrcodeEquipmentsWithArray:equipmentsArray];
}

- (YWMeterProperty *)meterPropertyWithEquipmentId:(NSString*)equipmentId{
    return [[YWDBTool shareDBTool] meterPropertyWithEquipmentId:equipmentId];
}

- (YWEquipment*)equipmentWithOwncode:(NSString*)ownCode
{
    return [[YWDBTool shareDBTool] equipmentWithOwncode:ownCode];
}

- (YWEquipmentType*)equipmentTypeWithEquipmentTypeId:(NSInteger)equipmentTypeId{
    return [[YWDBTool shareDBTool] equipmentTypeWithEquipmentTypeId:equipmentTypeId];
}

- (YWEquipment*)equipmentWithId:(NSString*)equipmentId
{
    return [[YWDBTool shareDBTool] equipmentWithId:equipmentId];
}

- (NSArray*)equipmentsOfMaintentanceMission:(YWMission*)mission
{
    return [[YWDBTool shareDBTool] equipmentsOfMaintentanceMission:mission];
}

- (NSString*)getTroubleReason:(YWMalfunctionRecordReasonId)troubleReason
{
    NSString* troubleReasonStr;
    switch (troubleReason) {
    case YWMalfunctionRecordReasonIdEnvironment:
        troubleReasonStr = @"环境";
        break;
    case YWMalfunctionRecordReasonIdNature:
        troubleReasonStr = @"自然损耗";
        break;
    case YWMalfunctionRecordReasonIdManMade:
        troubleReasonStr = @"人为";
        break;
    case YWMalfunctionRecordReasonIdQuality:
        troubleReasonStr = @"产品质量";
        break;
    case YWMalfunctionRecordReasonIdConstruction:
        troubleReasonStr = @"施工";
        break;
    case YWMalfunctionRecordReasonIdLeaveBehind:
        troubleReasonStr = @"遗留问题";
        break;
    case YWMalfunctionRecordReasonIdOther:
        troubleReasonStr = @"其他";
        break;
    default:
        break;
    }
    return troubleReasonStr;
}

//- (BOOL)saveLastMaintenanceTimeWithArray:(NSArray*)lastMaintenanceTimeArray
//{
//    return [[YWDBTool shareDBTool] saveLastMaintenanceTimeWithArray:lastMaintenanceTimeArray];
//}
//
//- (YWLastMaintenanceTime*)lastMaintenanceTimeWithEquipmentId:(NSString*)equipmentId
//{
//    return [[YWDBTool shareDBTool] lastMaintenanceTimeWithEquipmentId:equipmentId];
//}
//
//- (BOOL)updateLastMaintenanceTimeWithEquipments:(NSArray*)equipments maintenceTime:(NSTimeInterval)maintenceTime executorName:(NSString*)executorName
//{
//    return [[YWDBTool shareDBTool] updateLastMaintenanceTimeWithEquipments:equipments maintenceTime:maintenceTime executorName:executorName];
//}

- (NSArray*)qrcodeEquipmentInfos:(NSString*)qrcodeId
{
    return [[YWDBTool shareDBTool] qrcodeEquipmentInfos:qrcodeId];
}

- (NSArray*)qrcodeEquipmentInfos:(NSString*)qrcodeId orLocationId:(NSInteger)locationId equipmentTypeId:(NSInteger)equipmentTypeId
{
    return [[YWDBTool shareDBTool] qrcodeEquipmentInfos:qrcodeId orLocationId:locationId equipmentTypeId:equipmentTypeId];
}

- (NSArray*)equipmentWithQrcodeId:(NSString*)qrcodeId orLocationId:(NSInteger)locationId equipmentTypeId:(NSInteger)equipmentTypeId
{
    return [[YWDBTool shareDBTool] equipmentWithQrcodeId:qrcodeId orLocationId:locationId equipmentTypeId:equipmentTypeId];
}

- (NSArray*)equipmentWithQrcodeId:(NSString*)qrcodeId{
    return [[YWDBTool shareDBTool] equipmentWithQrcodeId:qrcodeId];
}

- (NSArray*)equipmentInNoQrcodeLedgerWithQrcodeId:(NSString*)qrcodeId{
    return [[YWDBTool shareDBTool] equipmentInNoQrcodeLedgerWithQrcodeId:qrcodeId];
}

- (NSArray*)equipmentTypeArrayWithLocationId:(NSInteger)locationId AndEquipmentTypeId:(NSInteger)equipmentTypeId{
    return [[YWDBTool shareDBTool] equipmentTypeArrayWithLocationId:locationId AndEquipmentTypeId:equipmentTypeId];
}

- (NSArray*)equipmentTypeArrayWithQrcodeId:(NSString*)qrcodeId{
    return [[YWDBTool shareDBTool] equipmentTypeArrayWithQrcodeId:qrcodeId];
}

- (NSArray*)equipmentArrayWithLocationId:(NSInteger)locationId{
    return [[YWDBTool shareDBTool] equipmentArrayWithLocationId:locationId];
}

- (NSArray*)equipmentWithLocationId:(NSInteger)locationId{
    return [[YWDBTool shareDBTool] equipmentWithLocationId:locationId];
}

- (NSArray *)equipmentsWithBuildingId:(NSString *)buildingId ApartmentId:(NSInteger)apartmentId floorId:(NSInteger)floorId locationId:(NSInteger)locationId eqipmentSubSystemType:(NSInteger)SubSystemTypeId equipmentTypeId:(NSInteger)equipmentTypeId page:(NSInteger)page{
    return [[YWDBTool shareDBTool] equipmentsWithBuildingId:buildingId ApartmentId:apartmentId floorId:floorId locationId:locationId eqipmentSubSystemType:SubSystemTypeId equipmentTypeId:equipmentTypeId page:page];
}

- (NSArray *)equipmentArrayWithBuildingId:(NSString *)buildingId ApartmentId:(NSInteger)apartmentId floorId:(NSInteger)floorId locationId:(NSInteger)locationId eqipmentSubSystemType:(NSInteger)SubSystemTypeId equipmentTypeId:(NSInteger)equipmentTypeId page:(NSInteger)page{
    return [[YWDBTool shareDBTool] equipmentArrayWithBuildingId:buildingId ApartmentId:apartmentId floorId:floorId locationId:locationId eqipmentSubSystemType:SubSystemTypeId equipmentTypeId:equipmentTypeId page:page];
}

- (NSArray*)equipmentArrayWithBuildingId:(NSString*)buildingId ApartmentId:(NSInteger)apartmentId floorId:(NSInteger)floorId locationId:(NSInteger)locationId eqipmentSubSystemType:(NSInteger)SubSystemTypeId equipmentTypeId:(NSInteger)equipmentTypeId{
    return [[YWDBTool shareDBTool] equipmentArrayWithBuildingId:buildingId ApartmentId:apartmentId floorId:floorId locationId:locationId eqipmentSubSystemType:SubSystemTypeId equipmentTypeId:equipmentTypeId];
}

- (NSArray *)equipmentInfoArrayWithBuildingId:(NSString *)buildingId ApartmentId:(NSInteger)apartmentId floorId:(NSInteger)floorId locationId:(NSInteger)locationId eqipmentSubSystemType:(NSInteger)SubSystemTypeId equipmentTypeId:(NSInteger)equipmentTypeId{
    return [[YWDBTool shareDBTool] equipmentInfoArrayWithBuildingId:buildingId ApartmentId:apartmentId floorId:floorId locationId:locationId eqipmentSubSystemType:SubSystemTypeId equipmentTypeId:equipmentTypeId];
}

- (NSArray*)equipmentTypesWithLocationId:(NSInteger)locationId  orBuildingId:(NSString*)buildingId{
    return [[YWDBTool shareDBTool] equipmentTypesWithLocationId:locationId orBuildingId:buildingId];
}

- (NSInteger)equipmentCountWithBuildingId:(NSString *)buildingId ApartmentId:(NSInteger)apartmentId floorId:(NSInteger)floorId locationId:(NSInteger)locationId eqipmentSubSystemType:(NSInteger)SubSystemTypeId equipmentTypeId:(NSInteger)equipmentTypeId{
    return [[YWDBTool shareDBTool] equipmentCountWithBuildingId:buildingId ApartmentId:apartmentId floorId:floorId locationId:locationId eqipmentSubSystemType:SubSystemTypeId equipmentTypeId:equipmentTypeId];
}
- (NSInteger)qrcodeCountWithBuildingId:(NSString *)buildingId ApartmentId:(NSInteger)apartmentId floorId:(NSInteger)floorId locationId:(NSInteger)locationId eqipmentSubSystemType:(NSInteger)SubSystemTypeId equipmentTypeId:(NSInteger)equipmentTypeId{
    return [[YWDBTool shareDBTool] qrcodeCountWithBuildingId:buildingId ApartmentId:apartmentId floorId:floorId locationId:locationId eqipmentSubSystemType:SubSystemTypeId equipmentTypeId:equipmentTypeId];
}

- (NSArray *)equipmentArrayOrderByLocationIdWithBuildingId:(NSString *)buildingId ApartmentId:(NSInteger)apartmentId floorId:(NSInteger)floorId locationId:(NSInteger)locationId eqipmentSubSystemType:(NSInteger)SubSystemTypeId equipmentTypeId:(NSInteger)equipmentTypeId  page:(NSInteger)page{
    return [[YWDBTool shareDBTool] equipmentArrayOrderByLocationIdWithBuildingId:buildingId ApartmentId:apartmentId floorId:floorId locationId:locationId eqipmentSubSystemType:SubSystemTypeId equipmentTypeId:equipmentTypeId page:page];
}
//- (void)requestLastMaintenanceTime
//{
//    NSDictionary *dic = [YWCommonTool addSubHttpHeadWithDic:[WMHttpTool shareHttpTool].HTTPHeaders];
//    [WMHttpTool shareHttpTool].HTTPHeaders = dic;
//    if (dic == nil) {return;}
//    [[WMHttpTool shareHttpTool] getWithUrl:[YWCommonTool getLastMaintenanceTimeUrl]
//        params:nil
//        success:^(NSHTTPURLResponse* response, id json) {
//            NSArray* lastMaintenanceTimes = [YWLastMaintenanceTime mj_objectArrayWithKeyValuesArray:json];
//            [[YWEquipmentManager shareManager] saveLastMaintenanceTimeWithArray:lastMaintenanceTimes];
//            [[YWNotifyManager shareManager] postLastMaintenanceTimeNotify];
//        }
//        failure:^(NSHTTPURLResponse* response, NSError* error) {
//            YWLog(@"%s,error:%@", __func__, error.localizedDescription);
//        }];
//}

- (NSInteger)locationIdWithQrcodeId:(NSString*)qrcodeId
{
    return [[YWDBTool shareDBTool] locationIdWithQrcodeId:qrcodeId];
}

- (BOOL)updateEquipmentUseMenthodWithArray:(NSArray*)useMenthodArray{
    return [[YWDBTool shareDBTool] updateEquipmentUseMenthodWithArray:useMenthodArray];
}
- (NSTimeInterval)useMenthodMaxUpdateTime{
    return [[YWDBTool shareDBTool] useMenthodMaxUpdateTime];
}
- (YWEquipmentUseMenthod*)useMenthodWithEquipmentTypeId:(NSInteger)equipmentTypeId{
    return [[YWDBTool shareDBTool] useMenthodWithEquipmentTypeId:equipmentTypeId];
}

- (BOOL)updateEquipmentUseRangeWithArray:(NSArray*)useRangeArray{
    return [[YWDBTool shareDBTool] updateEquipmentUseRangeWithArray:useRangeArray];
}
- (NSTimeInterval)useRangeMaxUpdateTime{
    return [[YWDBTool shareDBTool] useRangeMaxUpdateTime];
}
- (YWEquipmentUseRange*)useRangeWithEquipmentTypeId:(NSInteger)equipmentTypeId{
    return [[YWDBTool shareDBTool] useRangeWithEquipmentTypeId:equipmentTypeId];
}

@end
